import 'dart:js_interop';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:wch/model/test/event.dart';
import 'package:wch/model/test/state.dart';

import '../model/test/bloc.dart';

class TestPage extends StatefulWidget {
  const TestPage({super.key});

  @override
  State<TestPage> createState() => _TestPageState();
}

class _TestPageState extends State<TestPage> {
  @override
  Widget build(BuildContext context) {
    return BlocProvider(
        create: (ctx) => TestBloc()..add(InitEvent()),
      child: Builder(builder: (ctx)=>_buildPage(context),),
    );
  }

  Widget _buildPage(BuildContext context) {
    final bloc = BlocProvider.of<TestBloc>(context);

    return Scaffold(
      appBar: AppBar(
        title: Text('Bloc Demo'),
      ),
      body: Container(
        child: Center(child: BlocBuilder<TestBloc, TestState>(
          builder: (context, state) {
            return Text(
              '点击了${bloc.state.count} 次',
              style: TextStyle(color: Colors.blue, fontSize: 22),
            );
          },
        )),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          bloc.add(AddCountEvent());
        },
        child: Icon(
          Icons.add,
          size: 36,
        ),
      ),
    );
  }
}
